package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.service.SkuLadderService;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.sms.vo.SkuSaleInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }
    @Autowired
    SkuFullReductionService skuFullReductionService;
    @Autowired
    SkuLadderService skuLadderService;
    @Transactional(rollbackFor = ArithmeticException.class) //本地事务管理
    @Override
    public void saveSkuSaleInfo(SkuSaleInfoVo skuSaleInfoVo) {
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();

        BeanUtils.copyProperties(skuSaleInfoVo , skuBoundsEntity);
        //将skuBounds的work值计算处理
        Integer[] work = skuSaleInfoVo.getWork();
        skuBoundsEntity.setWork(work[0]*8+work[1]*4+work[2]*2+work[3]*1);
        //保存积分数据
        this.save(skuBoundsEntity);

        BeanUtils.copyProperties(skuSaleInfoVo , skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSaleInfoVo.getFullAddOther());
        //保存满减数据
        skuFullReductionService.save(skuFullReductionEntity);
        BeanUtils.copyProperties(skuSaleInfoVo , skuLadderEntity);
        skuLadderEntity.setAddOther(skuSaleInfoVo.getLadderAddOther());
        //保存折扣数据
        skuLadderService.save(skuLadderEntity);

//        int i = 1/0;
    }
    /*
        每个sku的营销数据存到了三张表中：
            SkuBoundsEntity、SkuFullReductionEntity、SkuLadderEntity
     */

    @Override
    public List<ItemSaleVo> queryItemSalesBySkuId(Long skuId) {
        List<ItemSaleVo> itemSaleVos = new ArrayList<>();
        SkuBoundsEntity skuBoundsEntity = this.getOne(new LambdaQueryWrapper<SkuBoundsEntity>()
                .eq(SkuBoundsEntity::getSkuId, skuId));
        if(skuBoundsEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc(String.format("购买送成长积分%s " +
                    ",送购物积分%s" ,skuBoundsEntity.getGrowBounds().toString(),
                    skuBoundsEntity.getBuyBounds().toString()));
            itemSaleVos.add(itemSaleVo);
        }

        SkuFullReductionEntity skuFullReductionEntity = skuFullReductionService.getOne(new LambdaQueryWrapper<SkuFullReductionEntity>()
                .eq(SkuFullReductionEntity::getSkuId, skuId));
        if(skuFullReductionEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc(String.format("购买满%s元,减%s元"
                    ,skuFullReductionEntity.getFullPrice().toString(),
                    skuFullReductionEntity.getReducePrice().toString()));
            itemSaleVos.add(itemSaleVo);
        }
        SkuLadderEntity skuLadderEntity = skuLadderService.getOne(new LambdaQueryWrapper<SkuLadderEntity>()
                .eq(SkuLadderEntity::getSkuId, skuId));
        if(skuLadderEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("折扣");
            itemSaleVo.setDesc(String.format("购买满%s件，打%s %s折扣"
                    ,skuLadderEntity.getFullCount().toString(),
                    skuLadderEntity.getDiscount().toString(),"%"));
            itemSaleVos.add(itemSaleVo);
        }

        return itemSaleVos;
    }

}